package cn.sdfykjyxgs.synserver.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.watch.SimpleWatcher;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.io.watch.watchers.DelayWatcher;
import cn.hutool.core.lang.Console;
import cn.sdfykjyxgs.syncommon.entity.FileChangeLog;
import cn.sdfykjyxgs.syncommon.service.FileChangeLogService;
import cn.sdfykjyxgs.syncommon.socket.WebSocketMessage;
import cn.sdfykjyxgs.synserver.common.WebSocketServer;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.util.Arrays;

public class FileListening {
    FileChangeLogService fileChangeLogService;

    public FileListening(FileChangeLogService fileChangeLogService) {
        this.fileChangeLogService = fileChangeLogService;
    }

    //可以写到配置文件中
    private static final String REQUEST_BASE_PATH = "D:\\test";

    public void startWatch() {
        File file = FileUtil.file(REQUEST_BASE_PATH);
        WatchMonitor monitor = null;
        monitor = WatchMonitor.createAll(file, new DelayWatcher(new Watcher() {
            @Override
            public void onCreate(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("创建：{}-> {}", currentPath, obj);
                String ulr = currentPath.toString();
                String name = obj.toString();
               /* boolean existed = fileChangeLogService.fileExisted(ulr, name);
                if (!existed) {
                    FileChangeLog fileChangeLog = fileChangeLogService.saveFileLog(ulr, name);
                    if (fileChangeLog.getId() != null)
                        WebSocketServer.sendMessage(new WebSocketMessage("", "", Arrays.asList(fileChangeLog)));

                }*/
            }

            @Override
            public void onModify(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("修改：{}-> {}", currentPath, obj);
            }

            @Override
            public void onDelete(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("删除：{}-> {}", currentPath, obj);
            }

            @Override
            public void onOverflow(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("Overflow：{}-> {}", currentPath, obj);
            }
        }, 0));
//        monitor = WatchMonitor.createAll(file, new SimpleWatcher() {
//            @Override
//            public void onModify(WatchEvent<?> event, Path currentPath) {
//                Object context = event.context();
//                Console.log("EVENT modify {}",context);
//            }
//
//            @Override
//            public void onCreate(WatchEvent<?> event, Path currentPath) {
//                super.onCreate(event, currentPath);
//                Object obj = event.context();
//                Console.log("onCreate：{}-> {}", currentPath, obj);
//            }
//
//            @Override
//            public void onDelete(WatchEvent<?> event, Path currentPath) {
//                super.onDelete(event, currentPath);
//                Object obj = event.context();
//                Console.log("onDelete：{}-> {}", currentPath, obj);
//            }
//
//            @Override
//            public void onOverflow(WatchEvent<?> event, Path currentPath) {
//                super.onOverflow(event, currentPath);
//                Object obj = event.context();
//                Console.log("onOverflow：{}-> {}", currentPath, obj);
//            }
//        });
        monitor.setMaxDepth(Integer.MAX_VALUE);
        monitor.start();
        //这里只监听文件或目录的修改事件
        /*WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
        watchMonitor.setWatcher(new Watcher() {
            @Override
            public void onCreate(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("创建：{}-> {}", currentPath, obj);
            }

            @Override
            public void onModify(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("修改：{}-> {}", currentPath, obj);
            }

            @Override
            public void onDelete(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("删除：{}-> {}", currentPath, obj);
            }

            @Override
            public void onOverflow(WatchEvent<?> event, Path currentPath) {
                Object obj = event.context();
                Console.log("Overflow：{}-> {}", currentPath, obj);
            }
        });

        //设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
        watchMonitor.setMaxDepth(3);*/
        //启动监听
//        watchMonitor.start();
    }
}
